package com.jiukuaidao.zhulin.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.TextView;
import android.widget.Toast;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author daifengming
 * @createdate 2014年12月19日
 * @description 字符串相关的工具类
 */
public class StringUtils {

	/**
	 * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
	 * 
	 * @param input
	 * @return boolean
	 */
	private final static SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
	private final static SimpleDateFormat dateFormater2 = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
	private final static SimpleDateFormat dateFormater4 = new SimpleDateFormat("MM-dd", Locale.CHINA);

	public static boolean isEmpty(String input) {
		if (input == null || "".equals(input))
			return true;

		for (int i = 0; i < input.length(); i++) {
			char c = input.charAt(i);
			if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
				return false;
			}
		}
		return true;
	}

	/**
	 * 返回唯一行标识（主键）
	 * 
	 * @return
	 */
	public static String getUUID() {
		UUID uuid = UUID.randomUUID();
		String str = uuid.toString();
		str = str.replaceAll("-", "");
		return str;
	}

	/**
	 * 对象转整数
	 * 
	 * @param obj
	 * @return 转换异常返回 0
	 */
	public static int toInt(Object obj) {
		if (obj == null)
			return 0;
		return toInt(obj.toString(), 0);
	}

	/**
	 * 字符串转整数
	 * 
	 * @param str
	 * @param defValue
	 * @return
	 */
	public static int toInt(String str, int defValue) {
		try {
			return Integer.parseInt(str);
		} catch (Exception e) {
		}
		return defValue;
	}

	/**
	 * 对象转整数
	 * 
	 * @param obj
	 * @return 转换异常返回 0
	 */
	public static long toLong(String obj) {
		try {
			return Long.parseLong(obj);
		} catch (Exception e) {
		}
		return 0;
	}

	/**
	 * 字符串转布尔值
	 * 
	 * @param b
	 * @return 转换异常返回 false
	 */
	public static boolean toBool(String b) {
		try {
			return Boolean.parseBoolean(b);
		} catch (Exception e) {
		}
		return false;
	}

	/**
	 * 获取当前日期
	 * 
	 * @return
	 */
	public static String nowDateTime() {
		Date curDate = new Date(System.currentTimeMillis());
		return dateFormater.format(curDate);
	}

	/**
	 * 使用delimiter来分割str，生成数组
	 * 
	 * @param str
	 * @param delimiter
	 * @return
	 */
	public static String[] delimitedListToStringArray(String str, String delimiter) {
		if (str == null) {
			return new String[0];
		}
		if (delimiter == null) {
			return new String[] { str };
		}

		List<String> result = new ArrayList<String>();
		if ("".equals(delimiter)) {
			result.add(str);
		} else {
			int pos = 0;
			int delPos = 0;
			while ((delPos = str.indexOf(delimiter, pos)) != -1) {
				result.add(str.substring(pos, delPos));
				pos = delPos + delimiter.length();
			}
			if (str.length() > 0 && pos <= str.length()) {
				// Add rest of String, but not in case of empty input.
				result.add(str.substring(pos));
			}
		}
		return result.toArray(new String[result.size()]);
	}

	// 判断str不是（null或者""）
	public static boolean hasLength(String str) {
		return (str != null && str.length() > 0);
	}

	/**
	 * 
	 * @param date
	 *            String yyyy-MM-dd HH:mm
	 * @param context
	 * @param view
	 *            EditText
	 */
	@SuppressLint("SimpleDateFormat")
	@SuppressWarnings("unused")
	public static void compareTime(String date, Context context, TextView view) {
		// 日期
		String dstime = date.split(" ")[0];
		// 时与分
		String mstime = date.split(" ")[1];
		// 年
		int ytime = Integer.valueOf(dstime.split("-")[0]);
		// 月
		int mtime = Integer.valueOf(dstime.split("-")[1]);
		// 日
		int dtime = Integer.valueOf(dstime.split("-")[2]);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date d;
		try {
			d = sdf.parse(date);
			Calendar calendar1 = Calendar.getInstance();
			calendar1.setTime(d);
			Calendar calendar2 = Calendar.getInstance();
			calendar2.setTime(new Date());
			if (calendar1.before(calendar2)) {
				Toast.makeText(context, "选择约酒时间不能小于当前时间", Toast.LENGTH_SHORT).show();
				view.setText("");
			} else {
				view.setText(formatCommonDate(date));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 将字符串转位日期类型
	 * 
	 * @param sdate
	 * @return
	 */
	public static Date toDate(String sdate) {
		try {
			return dateFormater.parse(sdate);

		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 以友好的方式显示时间。先按照时常判断 刚刚(2分钟内) n分钟前(1小时内),超出一小时按照日历判断 昨天 今天 月-日
	 * 
	 * @param sdate
	 * @return
	 */
	public static String friendly_time(String sdate) {
		Date time = toDate(sdate);// 传入时间
		if (time == null) {
			return "Unknown";
		}
		String ftime = "";
		Calendar cal = Calendar.getInstance();// 当前时间
		Calendar yescal = Calendar.getInstance();// 昨天时间
		yescal.add(Calendar.DAY_OF_MONTH, -1);

		// 判断是否是同一天
		String curDate = dateFormater2.format(cal.getTime());// 当前时间日期
		String yescurDate = dateFormater2.format(yescal.getTime());// 昨天日期
		String paramDate = dateFormater2.format(time);// 传入时间的日期
		int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);// 和当前相差小时数
		int minuts = (int) ((cal.getTimeInMillis() - time.getTime()) / 60000);// 和当前相差分钟数

		if (minuts < 2) {// 两分钟之内为“刚刚”
			ftime = "刚刚";
			return ftime;
		} else if (hour == 0) {// 2分钟~1小时为“..分钟前”
			ftime = Math.max(minuts, 2) + "分钟前";
			return ftime;
		}

		if (curDate.equals(paramDate)) {// 如果同一天
			ftime = "今天";
		} else if (yescurDate.equals(paramDate)) {// 如果是当前时间的昨天等于传入时间
			ftime = "昨天";
		} else {
			ftime = dateFormater4.format(time);
		}
		return ftime;
	}

	/**
	 * 字符串格式化为时间为 12:00 2014/09/15
	 * 
	 * @param date
	 *            日期：2014-09-15 14:02:58
	 * @return
	 */
	public static String formatDate(String date) {
		StringBuffer strBuff = new StringBuffer();
		// 获取ymd
		String[] ymd_hs = date.split(" ");
		if (2 == ymd_hs.length) {
			String ymd = ymd_hs[0];
			String hms = ymd_hs[1];
			String[] hour_second = hms.split(":");
			if (hour_second.length == 3) {
				String hour = hour_second[0];
				String minute = hour_second[1];
				String second = hour_second[2];
				if (Integer.valueOf(hour) < 10 && hour.length() == 1) {
					hour = "0" + hour;
				}
				if (Integer.valueOf(minute) < 10 && hour.length() == 1) {
					minute = "0" + minute;
				}
				if (Integer.valueOf(second) < 10 && hour.length() == 1) {
					second = "0" + second;
				}
				hms = hour + ":" + minute;
			}
			strBuff.append(hms);
			strBuff.append("  ");

			String[] year_month_day = ymd.split("-");
			if (3 == year_month_day.length) {
				// yeay
				String year = year_month_day[0];
				// month
				String month = year_month_day[1];
				// day
				String day = year_month_day[2];

				strBuff.append(year);
				strBuff.append("/");
				strBuff.append(month);
				strBuff.append("/");
				strBuff.append(day);
			}
		}
		return strBuff.toString();

	}

	/**
	 * 字符串格式化为时间为 12:00 2014/09/15
	 * 
	 * @param date
	 *            日期：2014-09-15 14:02:58
	 * @return
	 */
	public static String formatNoticeDate(String date) {
		StringBuffer strBuff = new StringBuffer();
		// 获取ymd
		String[] ymd_hs = date.split(" ");
		if (2 == ymd_hs.length) {
			String ymd = ymd_hs[0];
			String hms = ymd_hs[1];
			String[] year_month_day = ymd.split("-");
			if (3 == year_month_day.length) {
				// yeay
				String year = year_month_day[0];
				if (4 == year.length()) {
					year = year.substring(2, 4);
				}

				// month
				String month = year_month_day[1];
				// day
				String day = year_month_day[2];

				strBuff.append(year);
				strBuff.append("-");
				strBuff.append(month);
				strBuff.append("-");
				strBuff.append(day);
			}

			strBuff.append(" ");
			String[] hour_second = hms.split(":");
			if (hour_second.length == 3) {
				String hour = hour_second[0];
				String minute = hour_second[1];
				String second = hour_second[2];
				if (Integer.valueOf(hour) < 10 && hour.length() == 1) {
					hour = "0" + hour;
				}
				if (Integer.valueOf(minute) < 10 && hour.length() == 1) {
					minute = "0" + minute;
				}
				if (Integer.valueOf(second) < 10 && hour.length() == 1) {
					second = "0" + second;
				}
				hms = hour + ":" + minute;
			}
			strBuff.append(hms);

		}
		return strBuff.toString();

	}

	/**
	 * 字符串格式化为时间为 将选出的个位数时间化为10位
	 * 
	 * @param date
	 *            日期：2014-09-15 6:4---->2014-09-15 06:04
	 * @return
	 */
	public static String formatCommonDate(String date) {
		StringBuffer strBuff = new StringBuffer();
		// 获取ymd
		String[] ymd_hs = date.split(" ");
		if (2 == ymd_hs.length) {
			String ymd = ymd_hs[0];
			String[] year_month_day = ymd.split("-");
			if (year_month_day.length == 3) {
				strBuff.append(year_month_day[0]);
				strBuff.append("-");
				String month = year_month_day[1];
				String day = year_month_day[2];
				if (Integer.valueOf(month) < 10) {
					month = "0" + month;
				}
				strBuff.append(month);
				strBuff.append("-");
				if (Integer.valueOf(day) < 10) {
					day = "0" + day;
				}
				strBuff.append(day);
			} else {
				strBuff.append(ymd);
			}
			String hs = ymd_hs[1];
			String[] hour_second = hs.split(":");
			if (hour_second.length == 2) {
				String hour = hour_second[0];
				String second = hour_second[1];
				if (Integer.valueOf(hour) < 10) {
					hour = "0" + hour;
				}
				if (Integer.valueOf(second) < 10) {
					second = "0" + second;
				}
				hs = "  " + hour + ":" + second;
			}
			strBuff.append(hs);

		}

		return strBuff.toString();

	}

	/**
	 * 清除str前面的不可见字符
	 * 
	 * @param str
	 * @return
	 */
	public static String trimLeadingWhitespace(String str) {
		if (!hasLength(str)) {
			return str;
		}
		StringBuffer buf = new StringBuffer(str);
		while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
			buf.deleteCharAt(0);
		}
		return buf.toString();
	}

	/**
	 * emoji 表情过滤
	 * 
	 * @return
	 */
	public static InputFilter emojiFilter() {
		return new InputFilter() {

			Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
					Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

			@Override
			public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
				Matcher emojiMatcher = emoji.matcher(source);
				if (emojiMatcher.find()) {
					return "";
				}
				return null;
			}
		};
	}

	/**
	 * 半角转换为全角
	 * 
	 * @param input
	 * @return
	 */
	public static String ToDBC(String input) {
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
				c[i] = (char) (c[i] - 65248);
		}
		return new String(c);
	}

	/**
	 * 将服务器返回时间字符串转为所需格式, 2014-09-26 17:08
	 * 
	 * @param s
	 * @param containTime
	 *            是否要输出时间
	 * @return
	 */
	public static String formatDate2(String s, boolean containTime) {
		String[] parts1 = s.split(" ");
		StringBuffer b = new StringBuffer();
		if (parts1 != null && parts1.length > 0) {
			String[] parts2 = parts1[0].split("-");// 日期
			b.append(parts2[0] + "-" + parts2[1] + "-" + parts2[2]);
			if (containTime && parts1 != null && parts1.length > 1) {
				String[] parts3 = parts1[1].split(":");
				if (parts3 != null && parts3.length > 1) {
					b.append(" " + parts3[0] + ":" + parts3[1]);
				}
			}
		}
		return b.toString();
	}

	public static String getTimeStampDifference(long start_time, long end_time) {
		String seconds;
		Date date1 = new Date(start_time);
		Date date2 = new Date(end_time);
		long date_time1 = date1.getTime();
		long date_time2 = date2.getTime();
		seconds = (date_time2 - date_time1) / 1000 + "";
		return seconds;
	}

	/**
	 * 提供精确的小数位四舍五入处理。
	 * 
	 * @param v
	 *            需要四舍五入的数字
	 * @param scale
	 *            小数点后保留几位
	 * @return 四舍五入后的结果
	 */
	public static double round(double v, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	/**
	 * 提供精确的小数位四舍五入处理。
	 * 
	 * @param v
	 *            需要四舍五入的数字
	 * @param scale
	 *            小数点后保留几位
	 * @return 四舍五入后的结果
	 */
	public static float round(float v, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(Float.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).floatValue();
	}

	/**
	 * 小数后面自动去0
	 * 
	 * @param v
	 * @return
	 */
	public static String roundNoZero(double v) {
		String s = String.valueOf(round(v, 2));
		if (!TextUtils.isEmpty(s)) {
			while (s.endsWith("0")) {// 从后面截掉0
				s = s.substring(0, s.length() - 1);
			}
			if (s.endsWith(".")) {// 截掉小数点
				s = s.substring(0, s.length() - 1);
			}
		}
		return s;
	}

	/**
	 * 小数后面自动去0
	 * 
	 * @param v
	 * @return
	 */
	public static String roundNoZero(float v) {
		String s = String.valueOf(round(v, 2));
		if (!TextUtils.isEmpty(s)) {
			while (s.endsWith("0")) {// 从后面截掉0
				s = s.substring(0, s.length() - 1);
			}
			if (s.endsWith(".")) {// 截掉小数点
				s = s.substring(0, s.length() - 1);
			}
		}
		return s;
	}
}
